const { MilvusClient,DataType } = require('@zilliz/milvus2-sdk-node');
//创建客户端
const milvusClient = new MilvusClient({
    address: 'localhost:19530',
    timeout: 100000
})
const db_name = 'blog';
const collection_name = 'articles';
const field_name ='title_vector';
(async function () {
    try {
        //列出当前的哪些数据库
        const result = await milvusClient.listDatabases();
        console.log(result.db_names);//[ 'blog', 'default' ]
        const dbExsits = result.db_names.includes(db_name)
        if (!dbExsits) {
            //创建新的数据库
            await milvusClient.createDatabase({ db_name });
            console.log('数据库创建成功')
        }
        //切换到新的数据库
        await milvusClient.useDatabase(db_name);
        console.log('已经成功切换到了' + db_name);
        //获取blog数据库中的articles集合里的name_vector字段上的索引信息
        const indexInfo  = await milvusClient.describeIndex({
            collection_name,
            field_name
        });
        const indexExists = indexInfo.index_descriptions.length>0;
        if(indexExists){
            return;
        }
        //如果尚未在这个字段上创建索引的话
        await milvusClient.createIndex({
            collection_name,
            field_name
        });
        console.log('索引创建成功');
        const indexInfo2  = await milvusClient.describeIndex({
            collection_name,
            field_name
        });
        console.log(indexInfo2);
    } catch (error) {
        await milvusClient.closeConnection();
        process.exit(1)
    }

})()